package com.yellowpage.ca.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.ca.data.UserRole;
import com.yellowpage.ca.dao.UserRoleDao;

public class UserRoleServiceImpl extends BaseServiceImpl implements UserRoleService {
	  private UserRoleDao userRoleDao;
	
	  public void setUserRoleDao(UserRoleDao userRoleDao) {
		  this.userRoleDao = userRoleDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public UserRole getUserRole(Long id){
	      return userRoleDao.get(id);
	  }
	  
    public List<UserRole> getAllUserRole(){
        return userRoleDao.getAll();
    }
    
    public List<Map> queryUserRoleByPage(Map queryMap, PageRender pageRender) {		
        
		QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
				"SELECT a.USER_ROLE_ID , a.ROLE_ID , a.USER_ID ," +
				"       (SELECT NAME FROM CA_USER where USER_ID = a.USER_ID) as USER_NAME, " + 
				"       (SELECT NAME FROM CA_ROLE where ROLE_ID = a.ROLE_ID) as ROLE_NAME, " + 
				"       (select display_name from CA_USER where user_id = a.create_user) as create_user,"	+ 
				"       date_format(a.create_time,'%Y-%m-%d %T') as create_time   FROM CA_USER_ROLE a WHERE 1=1 ")
				    			.addCondition(" and USER_ROLE_ID = :userRoleId ")
							    			.addCondition(" and ROLE_ID = :roleId ")
							    			.addCondition(" and USER_ID = :userId ")
							    			.addCondition(" and CREATE_USER = :createUser ")
							    		    .addCondition(" and date_format(CREATE_TIME,'%Y-%m-%d') =  :createTime  ")
		    				    ; 		    
		    

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
    public Map getRoleMap() {
		Map roleMap = new LinkedMap();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("  select NAME, r.ROLE_ID as VALUE from CA_ROLE r ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		for (int i = 0; i < resultList.size(); i++) {
			roleMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("NAME"));
		}

		return roleMap;

	}

	public Map getUserMap() {
		Map userMap = new LinkedMap();

		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL("  select u.DISPLAY_NAME as NAME , u.USER_ID as VALUE " +
						"  from CA_USER u " +
						"  order by u.DISPLAY_NAME ");

		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());

		for (int i = 0; i < resultList.size(); i++) {
			userMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("NAME"));
		}

		return userMap;

	}
    
    
    public void saveUserRole(UserRole userRole){
        userRoleDao.save(userRole);
    }
    
	  public void updateUserRole(UserRole userRole){
	      userRoleDao.update(userRole);
	  }
	  
	  public void delete(List deleteList){
	      userRoleDao.remove(deleteList);
	  }
	  
	  
	 
	
}	